﻿using ImoviesBlazorAntPro.Server.Data;
using ImoviesBlazorAntPro.Shared.Entities;
using ImoviesBlazorAntPro.Shared.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ImoviesBlazorAntPro.Server.Repository
{
    public class AnnualReviewRepository : IAnnualReviewRepository
    {
        private readonly ApplicationDbContext _dbContext;

        public AnnualReviewRepository(ApplicationDbContext dbContext)
        {
            this._dbContext = dbContext;
        }

        /// <summary>
        /// 查询全部年度点评数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public Task<IEnumerable<AnnualReview>> GetListAsync(AnnualReviewsVideoCate videoCate)
        {
            return Task.FromResult(_dbContext.Set<AnnualReview>()
                .Where(x => x.VideoCate == (uint)videoCate)
                .AsEnumerable());
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="annualReview"></param>
        public void AddAnnualReview(AnnualReview annualReview)
        {
            _dbContext.Set<AnnualReview>().Add(annualReview);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="annualReview"></param>
        public async Task UpdateAnnualReviewAsync(AnnualReview annualReview)
        {
            var a = await _dbContext.Set<AnnualReview>().FindAsync(annualReview.Id);
            if (a != null)
            {
                _dbContext.Set<AnnualReview>().Attach(a);
                a.Year = annualReview.Year;
                a.Comment = annualReview.Comment;
            }
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<bool> SaveAsync()
        {
            return Task.FromResult(_dbContext.SaveChanges() > 0);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="annualReviewId"></param>
        public async Task DeleteAnnualReviewAsync(uint annualReviewId)
        {
            var a = await _dbContext.Set<AnnualReview>().FindAsync(annualReviewId);
            if (a != null)
            {
                _dbContext.Set<AnnualReview>().Remove(a);
            }
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        public void DeleteBatch(List<uint> Ids)
        {
            var list = _dbContext.Set<AnnualReview>().Where(x => Ids.Contains(x.Id)).ToList();
            if (list != null || list.Count > 0)
            {
                _dbContext.Set<AnnualReview>().RemoveRange(list);
            }
        }

    }
}
